package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.*;
import cn.wolfcode.wms.exception.LogicException;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.SaleAccountMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;

@Service
public class ProductStockServiceImpl implements IProductStockService {
    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private SaleAccountMapper saleAccountMapper;

    public void stockIncome(StockIncomeBill bill) {
        for (StockIncomeBillItem item : bill.getItems()) {
            //1.根据 商品+仓库 查询相对应的库存记录
            ProductStock ps = productStockMapper.selectByProductIdAndDepotId(item.getProduct().getId(), bill.getDepot().getId());
            //2.判断是否有库存记录,如果没有就新增一条
            if (ps == null) {
                ps = new ProductStock();
                ps.setProduct(item.getProduct());
                ps.setDepot(bill.getDepot());
                ps.setPrice(item.getCostPrice());
                ps.setAmount(item.getAmount());
                ps.setStoreNumber(item.getNumber());

                productStockMapper.insert(ps);
            } else {
                ps.setStoreNumber(ps.getStoreNumber().add(item.getNumber()));
                ps.setAmount(ps.getAmount().add(item.getAmount()));
                ps.setPrice(ps.getAmount().divide(ps.getStoreNumber(), 2, RoundingMode.HALF_UP));
                //3.否则更新库存记录的数量,价格,总额
                productStockMapper.updateByPrimaryKey(ps);
            }
        }
    }

    public void stockOutCome(StockOutcomeBill bill) {
        for (StockOutcomeBillItem item : bill.getItems()) {
            //1.根据商品id加库存id查询库存记录
            ProductStock ps = productStockMapper.selectByProductIdAndDepotId(item.getProduct().getId(), bill.getDepot().getId());
            //2.判断如果ps==null,直接提示用户在xx仓库中没有xx货品
            if (ps == null) {
                throw new LogicException("在[" + bill.getDepot().getName() + "]仓库中没有[" + item.getProduct().getName() + "]商品");

            }
            //如果有库存,在判断商品数量是否大于这次出库的数量
            if (ps.getStoreNumber().compareTo(item.getNumber()) < 0) {
                //如果不够,提示用户在仓库中xx货品的库存不足
                throw new LogicException("[" + item.getProduct().getName() + "]货品在[" + bill.getDepot().getName() + "]仓库中数量不足");
            }
            //将库存明细的数量减去出库的数量
            ps.setStoreNumber(ps.getStoreNumber().subtract(item.getNumber()));
            //重新计算库存总额
            ps.setAmount(ps.getPrice().multiply(ps.getStoreNumber().setScale(2, RoundingMode.HALF_UP)));
            //更新库存明细
            productStockMapper.updateByPrimaryKey(ps);

            //记录销售帐
            SaleAccount sa = new SaleAccount();
            sa.setClientId(bill.getClient().getId());
            sa.setCostPrice(ps.getPrice());
            sa.setNumber(item.getNumber());
            sa.setSalePrice(item.getSalePrice());
            sa.setSaleAmount(item.getAmount());
            sa.setCostAmount(sa.getNumber().multiply(sa.getCostPrice()).setScale(2,RoundingMode.HALF_UP));
            sa.setProductId(item.getProduct().getId());
            sa.setSaleManId(bill.getInputUser().getId());
            sa.setVdate(bill.getVdate());
            //保存销售帐
            saleAccountMapper.insert(sa);
        }
    }

    public PageResult query(QueryObject qo) {
        int count = productStockMapper.queryForCount(qo);
        if (count == 0) {
            return PageResult.empty(qo.getPageSize());
        }
        return new PageResult(count, productStockMapper.queryForList(qo), qo.getCurrentPage(), qo.getPageSize());
    }
}
